import 'package:carbon/network/user_api.dart';
import 'package:carbon/widgets/confirmDialog.dart';
import 'package:flutter/material.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  TextEditingController _accountNameController = TextEditingController();
  TextEditingController _pwdController = TextEditingController();
  GlobalKey _formKey =GlobalKey<FormState>();
  UserApi userApi = new UserApi();
  int selectRadioValue = 0;

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      body: Container(
          height: double.infinity,
          decoration: BoxDecoration(
              image: DecorationImage(
                  image: AssetImage("assets/imgs/login_bg.png"),
                  fit:BoxFit.fill
              )
          ),
          alignment: Alignment.center,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Container(
                margin: EdgeInsets.only(right: 30, left:30,top: 275),
                decoration: BoxDecoration(
                  // color: Colors.blueAccent
                ),
                child: Card(
                    surfaceTintColor: Colors.white,
                    shadowColor: Colors.grey.shade200,
                    elevation: 10,
                    shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
                    child: Container(
                        margin: EdgeInsets.all(10),
                        padding: EdgeInsets.only(bottom: 5),
                        child: Form(
                          key: _formKey,
                          child: Column(
                            children: [
                              Row(
                                children: [
                                  Expanded(
                                    child: TextFormField(
                                      controller: _accountNameController,
                                      decoration: InputDecoration(
                                          prefixIcon: Icon(Icons.account_circle_outlined,color: Colors.green,),
                                          hintText: "请输入用户名",
                                          border: InputBorder.none
                                      ),
                                      validator: (value){
                                        if(value!.trim().isEmpty){
                                          return "用户名不能为空";
                                        }
                                        return null;
                                      },
                                    ),
                                  )
                                ],
                              ),
                              Row(
                                children: [
                                  Expanded(
                                    child: TextFormField(
                                      controller: _pwdController,
                                      obscureText: true,
                                      decoration: InputDecoration(
                                          prefixIcon: Icon(Icons.lock_outline_rounded,color: Colors.green,),
                                          hintText: "请输入登录密码",
                                          border: InputBorder.none
                                      ),
                                      validator: (value){
                                        if(value!.isEmpty){
                                          return "密码不能为空";
                                        }
                                        return null;
                                      },
                                    ),
                                  )
                                ],
                              ),
                              Padding(padding: EdgeInsets.only(bottom: 5)),
                              Row(
                                mainAxisAlignment: MainAxisAlignment.end,
                                children: [
                                  Text("忘记密码?",style: TextStyle(color: Colors.amber.shade600),)
                                ],
                              ),
                              Padding(padding: EdgeInsets.only(bottom: 10)),
                              Row(
                                children: [
                                  Expanded(
                                    child: ElevatedButton(
                                        style: ButtonStyle(
                                          shape: MaterialStateProperty.all(RoundedRectangleBorder(borderRadius: BorderRadius.circular(25))),
                                          backgroundColor: MaterialStateColor.resolveWith((states) {
                                            //获取 焦点时/点击时 的颜色
                                            if (states.contains(MaterialState.focused) || states.contains(MaterialState.pressed)) {
                                              return Colors.green.shade500;
                                            }
                                            //默认状态使用灰色
                                            return Colors.green.shade300;
                                          }),
                                        ),
                                        onPressed: (){
                                          login();
                                        },
                                        child: Text("登 录",style: TextStyle(color: Colors.white, fontSize: 15, fontWeight: FontWeight.bold),)),
                                  )
                                ],
                              ),
                              Padding(padding: EdgeInsets.only(bottom: 3)),
                              Row(
                                children: [
                                  Expanded(
                                    child: ElevatedButton(
                                        style: ButtonStyle(
                                          shape: MaterialStateProperty.all(RoundedRectangleBorder(borderRadius: BorderRadius.circular(25))),
                                          backgroundColor: MaterialStateColor.resolveWith((states) {
                                            //获取 焦点时/点击时 的颜色
                                            if (states.contains(MaterialState.focused) || states.contains(MaterialState.pressed)) {
                                              return Colors.grey.shade300;
                                            }
                                            //默认状态使用灰色
                                            return Colors.grey.shade50;
                                          }),
                                        ),
                                        onPressed: (){
                                          Navigator.pushNamed(context, "register");
                                        },
                                        child: Text("去注册",style: TextStyle(color:Colors.green.shade300, fontSize: 15, fontWeight: FontWeight.bold),)),
                                  )
                                ],
                              ),
                            ],
                          ),
                        )
                    )
                ),
              ),
              Container(
                margin: EdgeInsets.only(bottom: 20),
                alignment: Alignment.center,
                child: Column(
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        // Padding(padding: EdgeInsets.only(right:10),),
                        Text.rich(
                            TextSpan(
                                children: [
                                  TextSpan(
                                    text: "碳信使",
                                    style: TextStyle(fontSize: 15,color: Colors.green),
                                  ),
                                  TextSpan(
                                    text: "为您服务",
                                    style: TextStyle(fontSize: 15),
                                  ),
                                ]
                            )
                        ),
                      ],
                    ),

                  ],
                ),
              )
            ],
          )
      ),
    );
  }

  void login() async{
    if ((_formKey.currentState as FormState).validate()) {
      var res = await userApi.userLogin(_accountNameController.text, _pwdController.text);
      if(res.code==200){
        await showConfirmDialog(context,"登录成功");
        Navigator.pushNamed(context, "index");
      }else{
        await showConfirmDialog(context, "登录失败，请重新再试");
      }
    }
  }
}
